﻿using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JT.WEB.Service;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NewsPublish.Model.Request;
using NewsPublish.Model.Response;

namespace JT.WEB.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class BannerController : Controller
    {
        private readonly BannerService _bannerService;
        private readonly IWebHostEnvironment _host;
        public BannerController(BannerService bannerService, IWebHostEnvironment hosting)
        {
            _bannerService = bannerService;
            _host = hosting;
        }
        // GET: Baner
        public ActionResult Index()
        {
            ResponseModel banner = _bannerService.GetBannerList();
            return View(banner);
        }

        // GET: Baner/Details/5
        public ActionResult BannerAdd()
        {
            return View();
        }
        //post
        [HttpPost]
        public async Task<JsonResult> AddBanner(AddBanner addBanner, IFormCollection collection)
        {
            if (collection is null || addBanner is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            IFormFileCollection files = collection.Files;
            if (files.Count > 0)
            {
                string webRootPath = _host.WebRootPath;
                string relativeDirPath = "\\BannerPic";
                string absolutePath = webRootPath + relativeDirPath;

                string[] fileType = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp", ".svg" };
                string extention = Path.GetExtension(files[0].FileName);
                if (fileType.Contains(extention.ToLower(CultureInfo.CurrentCulture)))
                {
                    if (!Directory.Exists(absolutePath))
                    {
                        Directory.CreateDirectory(absolutePath);
                    }
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + extention;
                    string filePath = absolutePath + "\\" + fileName;
                    using (FileStream stream = new FileStream(filePath, FileMode.Create))
                    {
                        await files[0].CopyToAsync(stream).ConfigureAwait(false);
                    }
                    addBanner.Image = "/BannerPic/" + fileName;
                    return Json(_bannerService.AddBanner(addBanner));
                }
                return Json(new ResponseModel { code = 0, result = "图片格式有误" });
            }
            return Json(new ResponseModel { code = 0, result = "请上传图片" });
        }
        //删除Banner
        [HttpPost]
        public JsonResult DelBanner(int id)
        {
            if (id <= 0)
            {
                return Json(new ResponseModel { code = 0, result = "参数有误" });
            }
            return Json(_bannerService.DeleteBanner(id));
        }
        // GET: Baner/Create
        public ActionResult Create()
        {
            return View();
        }






    }
}